/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.business.security.boundary;

import it.polimi.registration.business.security.entity.Event;
import it.polimi.registration.business.security.entity.MyForecast;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 *
 * @author Luca
 */
@Stateless
public class ForecastManager {
    
    @PersistenceContext
    EntityManager em;
    
    @Inject
    Principal principal;
    
    public void save(MyForecast myforecast) {

        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<MyForecast>> constraintViolations = validator.validate(myforecast);
        if (constraintViolations.size() > 0) {
            Iterator<ConstraintViolation<MyForecast>> iterator = constraintViolations.iterator();
            while (iterator.hasNext()) {
                ConstraintViolation<MyForecast> cv = iterator.next();
                System.err.println(cv.getRootBeanClass().getName() + "." + cv.getPropertyPath() + " " + cv.getMessage());

                //JsfUtil.addErrorMessage(cv.getRootBeanClass().getSimpleName() + "." + cv.getPropertyPath() + " " + cv.getMessage());
            }
        } else {
            em.persist(myforecast);
        }
    }

    public void delete() {
        em.remove(getSelectedForecast());
    }
    
    private Object getSelectedForecast() {
        return em.find(Event.class, principal.getName());
    }
    
    public List<MyForecast> getAllForecast(){
        Query query = em.createQuery("SELECT f FROM MyForecast f");
        List<MyForecast> result = new ArrayList<>(query.getResultList());
        return (List) result;
    } 
    
     public List<MyForecast> getForecastOfEvent(Event event){
        Query query = em.createQuery("SELECT f FROM MyForecast f Where f.location.city= :location and f.date= CAST(:date as DATE)");
        query.setParameter("location", event.getLocation().getCity());
        query.setParameter("date", event.getEndDate());
        List<MyForecast> result = new ArrayList<>(query.getResultList());
        return result;
    } 
    
    public void updateForecast(Event e, String description){
        Query query = em.createQuery("UPDATE MyForecast f SET f.description = :description WHERE(CAST(f.date as DATE)= :e.getEndDate() AND f.location.getCity()= :e.city)");
        query.setParameter("e.city", e.getLocation().getCity());
        query.setParameter("description", description);
        query.executeUpdate();
    }
}
